#!/usr/bin/env python3
+import os
import argparse
import sys
+import filecmp
+
+def replace_if_changed(new, old):
+ '''
+ Compare contents and only replace if changed to avoid triggering a rebuild.
+ '''
+ try:
+ changed = not filecmp.cmp(new, old, shallow=False)
+ except FileNotFoundError:
+ changed = True
+ if changed:
+ os.replace(new, old)
+ else:
+ os.remove(new)
parser = argparse.ArgumentParser()
parser.add_argument('--array-name', help='The name of the array variable')
-parser.add_argument('--output', metavar='FILE', help='Output file',
- type=argparse.FileType('w'),
- default=sys.stdout)
+parser.add_argument('--output', metavar='STRING', help='Output filename',
+ default=None)
parser.add_argument('input', metavar='FILE', help='The input file',
type=argparse.FileType('r'))
args = parser.parse_args()
-args.output.write('static const char {}[] = {{\n'.format(args.array_name))
-for line in args.input:
- for ch in line:
- args.output.write(' 0x{:02x},\n'.format(ord(ch)))
+if args.output is None:
+ output = sys.stdout
+else:
+ output = args.output + '~'
+
+with open(output, 'w') as f:
+ f.write('static const char {}[] = {{\n'.format(args.array_name))
+ for line in args.input:
+ for ch in line:
+ f.write(' 0x{:02x},\n'.format(ord(ch)))
+ f.write('};')
-args.output.write('};')
+if args.output is not None:
+ replace_if_changed(output, args.output)
# Usage: gen-gdk-gresources-xml SRCDIR_GDK [OUTPUT-FILE]
import os, sys
+import filecmp
+
+def replace_if_changed(new, old):
+ '''
+ Compare contents and only replace if changed to avoid triggering a rebuild.
+ '''
+ try:
+ changed = not filecmp.cmp(new, old, shallow=False)
+ except FileNotFoundError:
+ changed = True
+ if changed:
+ os.replace(new, old)
+ else:
+ os.remove(new)
srcdir = sys.argv[1]
if len(sys.argv) > 2:
outfile = sys.argv[2]
- f = open(outfile, 'w')
- f.write(xml)
- f.close()
+ tmpfile = outfile + '~'
+ with open(tmpfile, 'w') as f:
+ f.write(xml)
+ replace_if_changed(tmpfile, outfile)
else:
print(xml)
# Usage: gen-gsk-gresources-xml OUTPUT-FILE [INPUT-FILE1] [INPUT-FILE2] ...
import os, sys
+import filecmp
+
+def replace_if_changed(new, old):
+ '''
+ Compare contents and only replace if changed to avoid triggering a rebuild.
+ '''
+ try:
+ changed = not filecmp.cmp(new, old, shallow=False)
+ except FileNotFoundError:
+ changed = True
+ if changed:
+ os.replace(new, old)
+ else:
+ os.remove(new)
source_shaders = []
vulkan_compiled_shaders = []
if len(sys.argv) > 1 and sys.argv[1] != '-':
outfile = sys.argv[1]
- f = open(outfile, 'w')
- f.write(xml)
- f.close()
+ tmpfile = outfile + '~'
+ with open(tmpfile, 'w') as f:
+ f.write(xml)
+ replace_if_changed(tmpfile, outfile)
else:
print(xml)
# Usage: gen-gtk-gresources-xml SRCDIR_GTK [OUTPUT-FILE]
import os, sys
+import filecmp
+
+def replace_if_changed(new, old):
+ '''
+ Compare contents and only replace if changed to avoid triggering a rebuild.
+ '''
+ try:
+ changed = not filecmp.cmp(new, old, shallow=False)
+ except FileNotFoundError:
+ changed = True
+ if changed:
+ os.replace(new, old)
+ else:
+ os.remove(new)
srcdir = sys.argv[1]
if len(sys.argv) > 2:
outfile = sys.argv[2]
- f = open(outfile, 'w')
- f.write(xml)
- f.close()
+ tmpfile = outfile + '~'
+ with open(tmpfile, 'w') as f:
+ f.write(xml)
+ replace_if_changed(tmpfile, outfile)
else:
print(xml)
import sys
import re
import os
+import filecmp
+
+def replace_if_changed(new, old):
+ '''
+ Compare contents and only replace if changed to avoid triggering a rebuild.
+ '''
+ try:
+ changed = not filecmp.cmp(new, old, shallow=False)
+ except FileNotFoundError:
+ changed = True
+ if changed:
+ os.replace(new, old)
+ else:
+ os.remove(new)
debug = os.getenv('GTK_GENTYPEFUNCS_DEBUG') is not None
if debug: print (len(funcs), 'functions')
-ofile = open(out_file, "w")
-ofile.write(file_output)
-ofile.close()
+tmp_file = out_file + '~'
+with open(tmp_file, 'w') as f:
+ f.write(file_output)
+replace_if_changed(tmp_file, out_file)